using System;
using System.Collections.Generic;
using System.Text;
using UMC.WorkFlow;
using System.Collections;
using UMC.Data;
using System.IO;

namespace UMC.WorkFlow.Subassembly
{
    [UIDesign(Name = "审批人", Explain = "审批人结点")]
    public class Shape : UMC.WorkFlow.Shape
    {

        /// <summary>
        /// 多人的审批方式，0为水平，1为垂直
        /// </summary>
        [Design(Caption = "多人方式", ArgumentType = ArgumentType.NVSource, ArgumentValue = "串行,0,并行,1")]
        public int MultiplePattern
        {
            get; set;
        }
        [Design(Caption = "设置审批人", Explain = "设置流程的审批人")]
        public IdentitySetting IdentitySetting { get; set; }

        public override Func<object> GetInstance(string prototypeName)
        {
            switch (prototypeName)
            {
                case "IdentitySetting":
                    return () => new IdentitySetting();
                case "SQL":
                    return () => new SqlSetting();
                default:
                    return base.GetInstance(prototypeName);
            }
        }
        protected override void WriteProperty(TextWriter writer)
        {
            if (IdentitySetting != null)
            {
                writer.Write(',');
                JSON.Serialize("IdentitySetting", this.IdentitySetting, writer);
            }
            if (MultiplePattern != 0)
            {
                writer.Write(',');
                JSON.Serialize("MultiplePattern", this.MultiplePattern, writer);
            }
            base.WriteProperty(writer);
        }
        public override void Read(string key, object value)
        {
            switch (key)
            {
                case "IdentitySetting":
                    this.IdentitySetting = (IdentitySetting)value;
                    break;
                case "MultiplePattern":
                    this.MultiplePattern = Utility.Parse(value as string, 0);
                    break;
                default:
                    base.Read(key, value);
                    break;
            }
        }

        /// <summary>
        /// 处理激活后的方法
        /// </summary>
        /// <param name="line">最后呼叫成功的线路</param>
        /// <param name="task">作业</param>
        public override void Activation(UMC.WorkFlow.Line line, UMC.WorkFlow.TaskInstance task, TaskRuntime runtime)
        {
            var hash = new System.Collections.Hashtable(runtime.Items);

            hash["shape.id"] = this.Id;
            hash["line.id"] = line != null ? line.Id : "None";

            if (this.IdentitySetting != null)
            {
                this.Identity = new Identity();

                var names = SubassemblyHelper.Identity(this.IdentitySetting, hash, task, this, this.Id);
                var usernames = new List<String>();
                usernames.AddRange(names);
                if (usernames.Count > 0)
                {
                    this.Identity.Username = String.Format("{0}", usernames[0]);
                    usernames.RemoveAt(0);
                    if (usernames.Count > 0)
                    {

                        if (this.MultiplePattern == 1)
                        {
                            AppendShape(task, this, false, usernames.ToArray());
                        }
                        else
                        {
                            AppendShape(task, this, true, usernames.ToArray());
                        }
                    }
                }
                else
                {
                    this.Identity.Username = String.Empty;
                }
            }
            else if (this.Identity == null)
            {
                this.Identity = new Identity();
            }

        }

    }
}
